package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.conf.Constant;
import com.service.RoleService;
import com.util.AjaxResult;
import com.util.Empty;
import com.util.Num;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.*;
import java.util.List;

@Validated
@RestController
@RequestMapping("/role")
@Api(tags = {"角色"})
public class RoleController {

    private final static Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    RoleService roleService;

    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public AjaxResult save(
            @ApiParam(hidden = true)
            @RequestAttribute(name = Constant.UserId) String user_id,
            @ApiParam(hidden = true)
            @RequestAttribute(name = Constant.Nickname) String user_name,
            @ApiParam(value = "角色名称", type = "String", required = true)
            @Length(max = 16) @NotBlank
            @RequestParam(name = "role_name", required = false) String role_name,
            @ApiParam(value = "备注", type = "String")
            @Length(max = 16)
            @RequestParam(name = "remark", required = false) String remark) {
        return roleService.save(user_id, user_name, role_name, remark);
    }

    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public AjaxResult update(
            @ApiParam(hidden = true)
            @RequestAttribute(name = Constant.UserId) String user_id,
            @ApiParam(hidden = true)
            @RequestAttribute(name = Constant.Nickname) String user_name,
            @ApiParam(value = "角色id", required = true)
            @Empty(max = 32)
            @RequestParam(name = "role_id", required = false) String role_id,
            @ApiParam(value = "角色名称", required = true)
            @Empty(max = 16)
            @RequestParam(name = "role_name", required = false) String role_name,
            @ApiParam(value = "备注", type = "String")
            @Length(max = 16)
            @RequestParam(name = "remark", required = false) String remark) {
        return roleService.update(user_id, user_name, role_id, role_name, remark);
    }

    @ApiOperation(value = "配置菜单")
    @PutMapping("/configMenus")
    public AjaxResult configMenus(
            @ApiParam(value = "角色id", required = true)
            @RequestParam(name = "role_id",required = false)
            @NotBlank @Length(max = 32) String role_id,
            @ApiParam(value = "菜单id ['菜单id']", required = false)
            @RequestParam(required = false) String menu_id) {
        List<String> list = JSONArray.parseArray(menu_id, String.class);
        return roleService.configMenus(role_id, list);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/del")
    public AjaxResult del(@ApiParam(value = "角色id", type = "String", required = true)
                          @RequestParam(name = "role_id", required = false)
                          @Empty(max = 32) String role_id) {
        return roleService.del(role_id);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping("/list")
    public AjaxResult list(String role_name,
                           @ApiParam(value = "当前页", required = true)
                           @RequestParam(name = "pageNum", required = false)
                           @Num(message = "当前页不可用") String pageNum,
                           @ApiParam(value = "每页条数", required = true)
                           @RequestParam(name = "pageSize", required = false)
                           @Num(message = "每页条数不可用") String pageSize) {
        return roleService.list(role_name, pageNum, pageSize);
    }

    @ApiOperation(value = "指定角色菜单")
    @GetMapping("/listMenuId")
    public AjaxResult listMenuId(
            @ApiParam(value = "角色id", required = true)
            @RequestParam(name = "role_id",required = false)
            @Empty(max = 32) String role_id) {
        return roleService.listMenuId(role_id);
    }

    @ApiOperation(value = "全部下拉列表")
    @GetMapping("/listAllSelect")
    public AjaxResult listAllSelect() {
        return roleService.listAllSelect();
    }

}
